package com.nega.modules.controller;

import com.nega.commons.model.MemberUser;
import com.nega.commons.model.SearchResult;
import com.nega.modules.service.ConfigService;
import com.nega.modules.service.SearchCenterService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @Author: Garcia
 * @CreateDate: 2019/5/9 16:35
 * @Description: 搜索中心
 */
@Controller
@RequestMapping("center")
@Slf4j
public class SearchCenterController {

    @Resource
    private SearchCenterService searchCenterService;

    @Resource
    private ConfigService configService;

    @GetMapping("search")
    public String search(Model model, String searchValue) {
        List<SearchResult> results = searchCenterService.search(searchValue);
        model.addAttribute("result", results);
        MemberUser memberUser = (MemberUser) SecurityUtils.getSubject().getPrincipal();
        if (memberUser != null) {
            model.addAttribute("user", memberUser);
            if (memberUser.getIsVip()) {
                String fileName = searchCenterService.PDFCreate(results);
                model.addAttribute("pdfName", fileName);
            }
        }
        return "view/searchlist/listinfo";
    }

    @GetMapping("dowload")
    public void download(String fileName, HttpServletResponse response) {
        if (StringUtils.isBlank(fileName)) {
            return;
        }

        String pdfpath = configService.getProperty("pdfPath");
        File file = new File(pdfpath + File.separator + fileName);
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            OutputStream outputStream = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                outputStream.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (Exception e) {
            log.error("下载异常", e);
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    log.error("关闭流异常", e);
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    log.error("关闭流异常", e);
                }
            }
        }
    }
}
